package cn.qn.controller.login;


import cn.qn.framwork.security.MemberAccountService;
import cn.qn.moudle.security.MemberAccount;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Slf4j
public class LoginController {

    @Autowired
    MemberAccountService memberAccountService;

    @RequestMapping(value = "/login")
    @ResponseBody
    public Object login(@AuthenticationPrincipal MemberAccount memberAccount) {
        if (memberAccount != null) {
            MemberAccount account = memberAccountService.getUserByUserId(memberAccount.getUserId());
            String password = memberAccount.getPassword();
            if (StringUtils.isNotBlank(password)) {
                if (new BCryptPasswordEncoder().matches(password, memberAccount.getPassword())) {
                    return memberAccount;
                } else {
                    return null;
                }
            } else {
                return account;
            }
        }
        return null;
    }

}
